﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Minecraft_Xenon
{
    public class PanelSwitchFX
    {
        //从左到右移动
        public const int SWITCH_TO_LEFT = 0;
        //从右到左移动
        public const int SWITCH_TO_RIGHT = 1;

        private Form Form;
        private Panel SourcePanel;
        private Panel TargetPanel;
        private int Mode;
        private int Speed;
        private int TargetSpeed;

        private System.Timers.Timer Timer;

        private int SourceX;
        private int SourceY;
        private int SourceWidth;
        private int SourceHeight;
        private Point SourceLocation;
        private int TargetX;
        private int TargetY;
        private int TargetWidth;
        private int TargetHeight;

        /// <summary>
        /// 面板
        /// </summary>
        /// <param name="form"></param>父窗体
        /// <param name="source"></param>原面板
        /// <param name="target"></param>需要切换的面板
        /// <param name="mode"></param>模式
        /// <param name="speed"></param>切换速度
        public PanelSwitchFX(Form form ,Panel source, Panel target, int mode, int speed)
        {
            Form = form;
            SourcePanel = source;
            TargetPanel = target;
            Mode = mode;
            Speed = speed;
            TargetSpeed = speed;

            SourceX = SourcePanel.Location.X;
            SourceY = SourcePanel.Location.Y;
            SourceWidth = SourcePanel.Size.Width;
            SourceHeight = SourcePanel.Size.Height;
            SourceLocation = SourcePanel.Location;
            TargetX = TargetPanel.Location.X;
            TargetY = TargetPanel.Location.Y;
            TargetWidth = TargetPanel.Size.Width;
            TargetHeight = TargetPanel.Size.Height;
        }

        //开始动画
        public void Show()
        {
            switch (Mode)
            {
                case SWITCH_TO_LEFT:
                    TargetPanel.Location = new Point(SourceX + SourceWidth, SourceY);
                    TargetX = TargetPanel.Location.X;
                    break;
                case SWITCH_TO_RIGHT:
                    TargetPanel.Location = new Point(1 - SourceWidth, SourceY);
                    TargetX = TargetPanel.Location.X;
                    break;
            }

            //每34毫秒一帧，约为30FPS
            Timer = new System.Timers.Timer(34);
            Timer.Elapsed += new System.Timers.ElapsedEventHandler(DoSwitch);
            Timer.Enabled = true;
        }

        private void DoSwitch(Object obj, System.Timers.ElapsedEventArgs e)
        {
            switch (Mode)
            {
                case SWITCH_TO_LEFT:

                    //移动面板
                    SourcePanel.Location = new Point(SourceX - Speed, SourceY);
                    SourceX = SourcePanel.Location.X;
                    TargetPanel.Location = new Point(TargetX - TargetSpeed, SourceY);
                    TargetX = TargetPanel.Location.X;
                    Form.Update();

                    //当超出界限时停止移动
                    if (SourceX + SourceWidth <= 1)
                    {
                        Speed = 0;
                    }
                    if (TargetX <= 1)
                    {
                        TargetSpeed = 0;
                    }

                    //停止时修正目标面板坐标
                    if (Speed == 0 && TargetSpeed == 0)
                    {
                        TargetPanel.Location = SourceLocation;
                        //停止计时器
                        Timer.Stop();
                    }


                    break;
                case SWITCH_TO_RIGHT:

                    //移动面板
                    SourcePanel.Location = new Point(SourceX + Speed, SourceY);
                    SourceX = SourcePanel.Location.X;
                    TargetPanel.Location = new Point(TargetX + TargetSpeed, SourceY);
                    TargetX = TargetPanel.Location.X;
                    Form.Update();

                    //当超出界限时停止移动
                    if (SourceX >= 760)
                    {
                        Speed = 0;
                    }
                    if (TargetX + TargetWidth >= 1)
                    {
                        TargetSpeed = 0;
                    }

                    //停止时修正目标面板坐标
                    if (Speed == 0 && TargetSpeed == 0)
                    {
                        TargetPanel.Location = SourceLocation;
                        //停止计时器
                        Timer.Stop();
                    }

                    break;
            }
        }
    }
}
